package org.spring.springboot.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spring.springboot.dao.CityDao;
import org.spring.springboot.domain.City;
import org.spring.springboot.ex.BpEx;
import org.spring.springboot.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

/** 城市业务逻辑实现类 */
@Service
public class CityServiceImpl implements CityService {

  private static final Logger LOGGER = LoggerFactory.getLogger(CityServiceImpl.class);

  @Autowired private CityDao cityDao;

  /** 获取城市逻辑： 如果缓存存在，从缓存中获取城市信息 如果缓存不存在，从 DB 中获取城市信息，然后插入缓存 */
  @CachePut(value = "city", key = "#id")
  public City findCityById(Long id) {
    City city = cityDao.findById(id);
    return city;
  }

  @Override
  @CachePut(value = "city", key = "#city.getId()")
  public Long saveCity(City city) {
    return cityDao.saveCity(city);
  }

  /** 更新城市逻辑： 如果缓存存在，删除 如果缓存不存在，不操作 */
  @Override
  @CachePut(value = "city", key = "#city.getId()")
  public Long updateCity(City city) {
    Long ret = cityDao.updateCity(city);
    return ret;
  }

  @Override
  @CacheEvict(value = "city", key = "#id")
  public Long deleteCity(Long id) {
    Long ret = cityDao.deleteCity(id);
    return ret;
  }
}
